package com.ruoyi.system.service.front.impl;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.CaseInfo;
import com.ruoyi.system.domain.CasePic;
import com.ruoyi.system.domain.StudentInfo;
import com.ruoyi.system.domain.front.CaseHistoryFront;
import com.ruoyi.system.domain.front.CaseInfoInsertFront;
import com.ruoyi.system.domain.front.CaseinfoFront;
import com.ruoyi.system.mapper.CaseInfoMapper;
import com.ruoyi.system.mapper.CasePicMapper;
import com.ruoyi.system.mapper.front.CaseFrontMapper;
import com.ruoyi.system.service.front.ICaseFrontService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
@Service
public class CaseFrontServiceImpl extends CxBase implements ICaseFrontService {
    @Autowired
    private CaseFrontMapper caseFrontMapper;
    @Autowired
    private CaseInfoMapper caseInfoMapper;
    @Autowired
    private CasePicMapper casePicMapper;

    /**
     * 查询报警历史列表
     *
     * @param studentInfo 学生学号
     * @return 报警历史列表
     */
    public List<CaseHistoryFront> selectCaseHistoryFrontList(StudentInfo studentInfo){
        if(!checkNull(studentInfo.getStudentId())) {
            List<CaseHistoryFront> caseHistoryFronts = new ArrayList<>();
            startPage();
            caseHistoryFronts = caseFrontMapper.selectCaseHistoryFrontList(studentInfo);
            for(CaseHistoryFront caseHistoryFront:caseHistoryFronts){
                caseHistoryFront.setCaseFirstTime(StampToString(Long.valueOf(caseHistoryFront.getCaseFirstTime())));
            }
            return caseHistoryFronts;
        }
        else{
            return null;
        }
    }
    /**
     * 查询案件详细信息
     *
     * @param caseinfoFront 学生学号
     * @return 案件详细信息
     */
    public CaseinfoFront caseinfoFront(CaseinfoFront caseinfoFront){
        if(!checkNull(caseinfoFront.getCaseId())) {
            CaseinfoFront caseinfoFronts = caseFrontMapper.caseinfoFront(caseinfoFront);
            caseinfoFronts.setCaseFirstTime(StampToString(Long.valueOf(caseinfoFronts.getCaseFirstTime())));
            if(caseinfoFronts!=null){
                caseinfoFronts.setCasePic(caseFrontMapper.caseinfoFrontPic(caseinfoFronts));
            }
            return caseinfoFronts;
        }
        else{
            return null;
        }
    }

    /**
     * 添加报警信息
     *s
     * @param caseinfoFront 报警信息
     * @return int
     */
    public int insertCaseFront(CaseInfoInsertFront caseinfoFront){
        if(!checkNull(caseinfoFront.getCaseType())&&!checkNull(caseinfoFront.getCaseContent())&&!checkNull(caseinfoFront.getLongPhone())
        && !checkNull(caseinfoFront.getStudentClassname())&& !checkNull(caseinfoFront.getStudentCollege())&&!checkNull(caseinfoFront.getStudentId())&&!checkNull(caseinfoFront.getStudentName())
        )
        {

                caseinfoFront.setCaseId(getStamp().toString());
                caseinfoFront.setCaseFirstTime(getStamp());
                caseinfoFront.setCaseStatus(0l);
                if(caseinfoFront.getCasePic()!= null&&caseinfoFront.getCasePic().size()>0){
                    caseFrontMapper.insertcasepic(caseinfoFront);
                }
                return caseFrontMapper.insertcaseinfo(caseinfoFront);
            }

        else{
            return 0;
        }
    }
    /**
     * 更新报警信息
     *s
     * @param caseInfoInsertFront 报警信息
     * @return int
     */
    public int updateCaseFront(CaseInfoInsertFront caseInfoInsertFront){
        if(!checkNull(caseInfoInsertFront.getCaseId())||!checkNull(caseInfoInsertFront.getCaseType())&&!checkNull(caseInfoInsertFront.getCaseContent())&&!checkNull(caseInfoInsertFront.getLongPhone())
        && !checkNull(caseInfoInsertFront.getStudentClassname())&& !checkNull(caseInfoInsertFront.getStudentCollege())&&!checkNull(caseInfoInsertFront.getStudentId())
        &&!checkNull(caseInfoInsertFront.getStudentName())
        )
        {
            if(caseFrontMapper.selectCaseUseFront(caseInfoInsertFront).size()>0) {
                caseInfoInsertFront.setCaseReviseTime(getStamp());
                CasePic casePic = new CasePic();
                casePic.setCaseId(caseInfoInsertFront.getCaseId());
                List<CasePic> casePics = casePicMapper.selectCasePicList(casePic);
                for (CasePic casePic1 : casePics) {
                    casePicMapper.deleteCasePicById(casePic1.getId());
                }
                caseInfoInsertFront.setCasePic(caseInfoInsertFront.getCasePic());
                if (caseInfoInsertFront.getCasePic().size() > 0) {
                    caseFrontMapper.insertcasepic(caseInfoInsertFront);
                }
                return caseFrontMapper.updateCaseInfo(caseInfoInsertFront);
            }
            return 0;
        }
        else{
            return 0;
        }
    }
}
